//
//  UserAccount.swift
//  WeiBo test
//
//  Created by Aquarius_wing on 16/5/5.
//  Copyright © 2016年 Wing. All rights reserved.
//

import UIKit

/**
 1.遵守NSCoding协议
 2.实现两个方法
 */

class UserAccount: NSObject,NSCoding {
    //用于调用access_token,接口获取授权后的access token
    var access_token : String?
    //access_token的生命周期,单位是秒数
    var expires_in : NSTimeInterval=0 {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //过期日期
    var expires_date:NSDate?
    //当前授权用户的UID
    var uid : String?
    //用户头像地址String
    var avatar_large : String?
    //用户名
    var name : String?
    
    //KVC设置初值
    init(dict : [String:AnyObject]){
        //super 实例化
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {    }
    
    //要使打印该类为字符串
    override var description: String{
        let keys=["access_token","expires_in","uid","avatar_large","name","expires_date"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //对外提供归档方法
    func saveAccount(){
        //归档
        let path=(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    
    //对外提供解归档方法
    //func 是一个对象方法 函数前 加 class表示是 类方法
    class func loadAccount() ->UserAccount? {
        //解归档
        let path=(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //判断一下,就一定有值
        if let account=NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount{
            //判断一下用户的token是否过期
            //OrderedAscending增序
            if account.expires_date?.compare(NSDate()) ==  NSComparisonResult.OrderedDescending{
                //截止日期比当前日期要小
                return account
            }
            print("过期了")
        }
        return nil
    }
    
    //MARK: 实现NSCoding 协议方法
    //解归档 将磁盘中的二进制数据 -> 转换成自定义对象 跟反序列化很像
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    //归档 将自定义对象 -> 转成换磁盘中的二进制数据 跟序列化像
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }
}
